// components/common/share-distributor/index.js
import componentBehavior from '../../../behaviors/component';
import { COMPONENT_OPTIONS } from '../../../constants/common';
import { downloadFile, saveImage, tipSuccess, tipError } from '../../../common/env';
import { getDistributorShareInfo, setDistributorShareInfo } from '../../../biztools/wex';
import { getQRcode } from '../../../api/common';

Component({
    behaviors: [componentBehavior],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        show: {
            type: Boolean,
            value: false,
        },
        shareDistributorInfo: {
            type: Object,
            value: null,
        },
        shareDistributorMessageInfo: {
            type: Object,
            value: null,
        },
    },
    observers: {
        'shareDistributorInfo': function(val) {
            if (val) {
                const _info = getDistributorShareInfo();
                const title = _info ? _info.title : `@${val.name} 邀请你成为${val.title || '分销员'}`;
                const image = _info.image;
                const current = _info.image ? 1 : 0;
                this.setData({
                    shareData: { title, image, current },
                });
                this._handleChange({ title, image: image || val.homepageBg });
            }
        },
    },
    data: {
        shareData: {},
        titleModal: {
            show: false,
        },
    },
    methods: {
        async _handleGetQrcode() {
            const { path } = this.data.shareDistributorMessageInfo;
            const pathArr = path.replace(/^\//, '').split('?');
            const url = await getQRcode({
                page: pathArr[0],
                scene: pathArr[1],
            });
            return url;
        },
        async _handleDownload() {
            const url = await this._handleGetQrcode();
            const file = await downloadFile(url);
            const flag = await saveImage(file.tempFilePath);
            flag ? tipSuccess('保存成功！') : tipError('保存失败！');
        },
        _handleChange(info) {
            this.triggerEvent('change', info);
        },
        handleSelect(e) {
            const { type } = e.currentTarget.dataset;
            if (type === 'download') {
                this._handleDownload();
            }
            this.handleClose();
        },
        handleClose(e) {
            this.setData({
                show: false,
            });
            this.triggerEvent('close');
        },
        handleShowTitleModal() {
            this.setData({
                'titleModal.show': true,
            });
        },
        handleChangeTitle(e) {
            const title = e.detail;
            this.setData({
                'shareData.title': e.detail,
            });
            setDistributorShareInfo({ title });
            this._handleChange({ title });
        },
        handleChangePic(e) {
            const image = e.detail;
            this.setData({
                'shareData.image': image,
            });
            this._handleChange({ image });
            setDistributorShareInfo({ image });
        },
        handleChangeType(e) {
            const { current } = e.detail;
            const { homepageBg } = this.data.shareDistributorInfo;
            const { image } = this.data.shareData;
            if (current === 0) {
                this._handleChange({ image: homepageBg });
            } else if (current === 1 && image) {
                this._handleChange({ image });
            }
        },
    }
})
